package com.qingcheng.service.impl;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.pojo.goods.Spu;
import com.qingcheng.service.goods.SpuService;
import com.qingcheng.service.AddAndDeleteEsService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wyj
 * @date 2019/7/10
 */

@Service(timeout = 3000)
public class AddAndDeleteEsServiceImpl implements AddAndDeleteEsService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Reference
    private SpuService spuService;

    @Override
    public void addEs(Spu Tspu) {


        //1.查询商品信息
        Goods goods = spuService.findGoodsById(Tspu.getId());
        //获取spu信息
        Spu spu = goods.getSpu();
        //获取sku列表
        List<Sku> skuList = goods.getSkuList();


        System.out.println(skuList.size());
        //封装对象
        BulkRequest bulkRequest = new BulkRequest();


        for (int i = 0; i < skuList.size(); i++) {
            Sku sku = skuList.get(i);
            System.out.println(i);
            IndexRequest indexRequest = new IndexRequest("sku","doc",sku.getId());

            Map skuMap =new HashMap();
            skuMap.put("name",sku.getName());
            skuMap.put("brandName",sku.getBrandName());
            skuMap.put("categoryName",sku.getCategoryName());
            skuMap.put("price",sku.getPrice());
            skuMap.put("createTime",sku.getCreateTime());
            skuMap.put("saleNum",sku.getSaleNum());
            skuMap.put("commentNum",sku.getCommentNum());
            skuMap.put("image",sku.getImage());
            Map spec=new HashMap();

            skuMap.put("spec", JSON.parseObject(sku.getSpec()));
            indexRequest.source(skuMap);

            bulkRequest.add(indexRequest);//可以添加多个


        }

        try {

            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);


        } catch (IOException e) {
            e.printStackTrace();
            //记录日志
        }


    }


    public void deleteEs(Spu Tspu) {

        //1.查询商品信息
        Goods goods = spuService.findGoodsById(Tspu.getId());
        //获取spu信息
        Spu spu = goods.getSpu();
        //获取sku列表
        List<Sku> skuList = goods.getSkuList();


        for (Sku sku : skuList) {
            //        封装删除 条件
            DeleteRequest deleteRequest = new DeleteRequest("sku","doc",sku.getId());
            DeleteResponse response = null;
            try {
                response = restHighLevelClient.delete(deleteRequest);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(response);

//        封装删除 条件


        }




    }
}
